//
// Created by Kame.Liang on 2021/9/4.
//

#ifndef DESIGNERDEMO_LAZYSINGLETON_H
#define DESIGNERDEMO_LAZYSINGLETON_H
#include <mutex>
template <class T>
class LazySingleton{
public:
    static T* GetInstance(){
        if(nullptr == t_){
            mtx_.lock();
            if (nullptr == t_)
                t_ = new (std::nothrow) T;
            mtx_.unlock();
        }
        return t_;
    }
private:
    static T* t_;
    static std::mutex mtx_;
};
template <class T>
T* LazySingleton<T>::t_ = nullptr;
template <class T>
std::mutex LazySingleton<T>::mtx_;

#endif //UNTITLED1_LAZYSINGLETON_H
